﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class BalanceCenter_GoodType_Edit : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ReadAction();
            BindData();
        }
    }

    #region 窗体设计器生成的代码

    protected override void OnInit(EventArgs e)
    {
        isRequestLogin = true;
        RequestLoginPage = "BalanceCenter/GoodType_Edit.aspx";

        base.OnInit(e);
    }

    #endregion

    protected void btnOK_Click(object sender, EventArgs e)
    {
        if (tbTypeName.Text.Trim() == "")
        {
            ShoveEL._Web.JavaScript.Alert(this.Page, "请输入商品名称。");
            return;
        }

        //if (tbTypeNorm.Text.Trim() == "")
        //{
        //    ShoveEL._Web.JavaScript.Alert(this.Page, "请输入类别属性。");
        //    return;        
        //}

        if (tbPrice.Text.Trim() == "")
        {
            ShoveEL._Web.JavaScript.Alert(this.Page, "请输入消费者购买价格。");
            return;
        }
        else
        {
            if (ShoveEL._Convert.StrToDouble(tbPrice.Text.Trim(), -1) <= 0)
            {
                ShoveEL._Web.JavaScript.Alert(this.Page, "请输入正确的价格。");
                return;
            }
        }

        //if (tbPrice2.Text.Trim() == "")
        //{
        //    ShoveEL._Web.JavaScript.Alert(this.Page, "请输入业务员购买价格。");
        //    return;
        //}
        //else
        //{
        //    if (ShoveEL._Convert.StrToDouble(tbPrice2.Text.Trim(), -1) <= 0)
        //    {
        //        ShoveEL._Web.JavaScript.Alert(this.Page, "请输入正确的价格。");
        //        return;
        //    }
        //}

        //if (tbPrice3.Text.Trim() == "")
        //{
        //    ShoveEL._Web.JavaScript.Alert(this.Page, "请输入区域代理购买价格。");
        //    return;
        //}
        //else
        //{
        //    if (ShoveEL._Convert.StrToDouble(tbPrice3.Text.Trim(), -1) <= 0)
        //    {
        //        ShoveEL._Web.JavaScript.Alert(this.Page, "请输入正确的价格。");
        //        return;
        //    }
        //}

        //if (tbPrice4.Text.Trim() == "")
        //{
        //    ShoveEL._Web.JavaScript.Alert(this.Page, "请输入区域总代理购买价格。");
        //    return;
        //}
        //else
        //{
        //    if (ShoveEL._Convert.StrToDouble(tbPrice4.Text.Trim(), -1) <= 0)
        //    {
        //        ShoveEL._Web.JavaScript.Alert(this.Page, "请输入正确的价格。");
        //        return;
        //    }
        //}

        bool isFileUpload = false;  //是否上传
        string fileName = "";
        string url = "~/UpFiles/";

        if (File1.Value != "")
        {
            isFileUpload = true;

            if (ShoveEL._IO.File.UploadFile(this.Page, this.File1, url, ref fileName, "" ) < 0)
            {
                //ShoveEL._Web.JavaScript.Alert(this.Page, "文件上传失败");
            }
        }

        if (hID.Value != "")
        {
            try
            {
                int i = DAL.Tables.T_GoodType.Update("[ID]=" + hID.Value.Trim(), tbTypeName.Text.Trim(), true, fileName, isFileUpload, ddlUserType.SelectedValue, true, tbRemark.Text.Trim(), true, true, false, ShoveEL._Convert.StrToDouble(tbPrice.Text.Trim(), 0), true, cbLimit.Checked, true, cbVirtual.Checked, true, cbBV.Checked, true, ShoveEL._Convert.StrToDouble(tbPrice.Text.Trim(), 0), true, ShoveEL._Convert.StrToDouble(tbPrice.Text.Trim(), 0), true, ShoveEL._Convert.StrToDouble(tbPrice.Text.Trim(), 0),true, tbGoodNorm.Text.Trim(),true,tbGoodColor.Text.Trim(),true);

                if (i >= 0)
                {
                    ShoveEL._Web.JavaScript.Alert(this.Page, "修改成功!");
                }
            }
            catch { }
        }
        else
        {
            try
            {
                int i = DAL.Tables.T_GoodType.Insert(tbTypeName.Text.Trim(), true, fileName, true, ddlUserType.SelectedValue, true, tbRemark.Text.Trim(), true, true, false, ShoveEL._Convert.StrToDouble(tbPrice.Text.Trim(), 0), true, cbLimit.Checked, true, cbVirtual.Checked, true, cbBV.Checked, true, ShoveEL._Convert.StrToDouble(tbPrice.Text.Trim(), 0), true, ShoveEL._Convert.StrToDouble(tbPrice.Text.Trim(), 0), true, ShoveEL._Convert.StrToDouble(tbPrice.Text.Trim(), 0), true, tbGoodNorm.Text.Trim(),true, tbGoodColor.Text.Trim(),true);

                if (i >= 0)
                {
                    ShoveEL._Web.JavaScript.Alert(this.Page, "添加成功!");

                    tbTypeName.Text = "";
                    tbGoodNorm.Text = "";
                    tbGoodColor.Text = "";
                    tbPrice.Text = "";
                    //tbPrice2.Text = "";
                    //tbPrice3.Text = "";
                    //tbPrice4.Text = "";
                    tbRemark.Text = "";
                }
            }
            catch { }
        }
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("GoodType_Manager.aspx");
    }

    private void ReadAction()
    {
        this.hID.Value = ShoveEL._Web.Utility.GetRequest("id");
    }

    private void BindData()
    {
        if (this.hID.Value != "")
        {
            long ID = ShoveEL._Convert.StrToLong(hID.Value, -1);
            DataTable dt = null;

            try
            {
                dt = DAL.Tables.T_GoodType.Select("", "[ID]=" + ID, "");

                if (dt == null)
                {
                    PublicFunction.GoError(ErrorNumber.DataReadWrite, "数据读写错误", this.GetType().BaseType.FullName);
                    return;
                }

                tbTypeName.Text = dt.Rows[0]["TypeName"].ToString();
                ddlUserType.SelectedValue = dt.Rows[0]["TypeNorm"].ToString(); 
                tbGoodNorm.Text = dt.Rows[0]["GoodNorm"].ToString();
                tbGoodColor.Text = dt.Rows[0]["GoodColor"].ToString(); 
                tbRemark.Text = dt.Rows[0]["Remark"].ToString();
                tbPrice.Text = dt.Rows[0]["Price"].ToString();
                //tbPrice2.Text = dt.Rows[0]["Price2"].ToString();
                //tbPrice3.Text = dt.Rows[0]["Price3"].ToString();
                //tbPrice4.Text = dt.Rows[0]["Price4"].ToString();
                cbVirtual.Checked = ShoveEL._Convert.StrToBool(dt.Rows[0]["IsVirtualSales"].ToString(), false);
                cbBV.Checked = ShoveEL._Convert.StrToBool(dt.Rows[0]["IsBvSales"].ToString(), false);
                string fileName = dt.Rows[0]["TypePic"].ToString();

                bool IsVirtualSales = ShoveEL._Convert.StrToBool(dt.Rows[0]["IsVirtualSales"].ToString(), false);
                if (IsVirtualSales)
                {
                    cbVirtual.Checked = true;
                }
                else
                {
                    cbVirtual.Checked = false;
                }

                bool IsBvSales = ShoveEL._Convert.StrToBool(dt.Rows[0]["IsBvSales"].ToString(), false);
                if (IsBvSales)
                {
                    cbBV.Checked = true;
                }
                else
                {
                    cbBV.Checked = false;
                }
                                
                bool IsLimit = ShoveEL._Convert.StrToBool(dt.Rows[0]["IsLimit"].ToString(), false);
                if (IsLimit)
                {
                    cbLimit.Checked = true;
                }
                else
                {
                    cbLimit.Checked = false;
                }

                string url = "~/UpFiles/" + fileName;

                Image1.ImageUrl = url;

            }
            catch { }
        }
    }

}
